package com.zjsru.plan2023.other;

/**
 * @Author: CookLee
 * @Date: 2023/8/19
 * URL化
 * \
 * 输入："Mr John Smith    ", 13
 * 输出："Mr%20John%20Smith"
 */
public class ReplaceSpaces {
    
    private static final String  CONSTANT_EMPTY = " ";
    
    private static final String  CONSTANT_EMPTY_ASCII = "%20";
    
    /**
     * 模拟
     *
     * @param S      年代
     * @param length 长度
     * @return {@link String}
     */
    public String replaceSpaces(String S, int length) {
        String[] strings = new String[length];
        for (int i = 0; i < length; i++) {
            strings[i] = String.valueOf(S.charAt(i));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].equals(CONSTANT_EMPTY)) {
                strings[i] = CONSTANT_EMPTY_ASCII;
            }
            sb.append(strings[i]);
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        ReplaceSpaces replaceSpaces = new ReplaceSpaces();
        String s = "Mr John Smith    ";
        int length = 13;
        System.out.println(replaceSpaces.replaceSpaces(s, length));
    }
    
}
